package com.gupao.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

	/*
	 * @Override protected void configure(AuthenticationManagerBuilder auth) throws
	 * Exception { auth.inMemoryAuthentication()
	 * .withUser("user_1").password("12345").authorities("USER") .and()
	 * .withUser("user_2").password("12345").authorities("USER"); }
	 */
	
	@Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }
	
	@Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        AuthenticationManager manager = super.authenticationManagerBean();
        return manager;
    }

	@Bean
	@Override
	protected UserDetailsService userDetailsService() {
//		List<GrantedAuthority> authorities=new ArrayList<GrantedAuthority>();
//		GrantedAuthority authority=new SimpleGrantedAuthority("USER");
//		authorities.add(authority);
//		
//		List<UserDetails> users=new ArrayList<UserDetails>();
//		UserDetails user1=new User("user_1", "12345",authorities);
//		UserDetails user2=new User("user_2", "12345",authorities);
//		users.add(user1);
//		users.add(user2);
		InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
		manager.createUser(User.withUsername("user_1").password("123456").authorities("USER").build());
		manager.createUser(User.withUsername("user_2").password("123456").authorities("USER").build());
		return manager;
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.requestMatchers().anyRequest().and().authorizeRequests().antMatchers("/auth/**").permitAll();
	}

}
